/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package presentacion;
import negocio.ControladorUsuarios;
import Auxiliares.Contantes;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
/**
 *
 * @author oelizondo
 * 
 * CLASE SINGLETON
 */
public class PanelBienvenida extends javax.swing.JPanel implements Contantes{

    /**
     * Creamos la variable statica privada para poder hacer el singleton.
     */
    private static PanelBienvenida _Instancia;
    public PanelPrincipal _PanelPrincipal; 
    private ControladorUsuarios _ControladorUsuarios;

    private ActionListener listener = new ActionListener() {  
        public void actionPerformed(ActionEvent evt) {
            login();
        }  
    };
    
    public static PanelBienvenida getInstancia(){
        if(_Instancia == null)
        {
            _Instancia = new PanelBienvenida();
        }
        return _Instancia;
    }
    
    private PanelBienvenida() {
        initComponents();
        this.txtPass.addActionListener(listener);
        this.txtUser.addActionListener(listener);
        this.lblMensaje.setVisible(false);
    }
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnIngresar = new javax.swing.JButton();
        lblMensaje = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtUser = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtPass = new javax.swing.JPasswordField();
        jLabel5 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(new javax.swing.border.MatteBorder(null));
        setMaximumSize(new java.awt.Dimension(900, 700));
        setMinimumSize(new java.awt.Dimension(900, 700));
        setPreferredSize(new java.awt.Dimension(900, 700));
        setLayout(null);

        btnIngresar.setBackground(new java.awt.Color(205, 51, 51));
        btnIngresar.setText("Entrar al Sistema");
        btnIngresar.setName("btnIngresar");
        btnIngresar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnIngresarMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                btnIngresarMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                btnIngresarMouseExited(evt);
            }
        });
        btnIngresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIngresarActionPerformed(evt);
            }
        });
        add(btnIngresar);
        btnIngresar.setBounds(540, 630, 270, 40);

        lblMensaje.setText("Presione la tecla para ingresar");
        add(lblMensaje);
        lblMensaje.setBounds(340, 640, 217, 14);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel3.setText("Usuario:");
        jLabel3.setName("jLabel3");
        add(jLabel3);
        jLabel3.setBounds(430, 510, 110, 22);

        txtUser.setText("admin");
        txtUser.setName("txtUser");
        add(txtUser);
        txtUser.setBounds(540, 510, 270, 30);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel4.setText("Password:");
        jLabel4.setName("jLabel3");
        add(jLabel4);
        jLabel4.setBounds(430, 560, 100, 22);

        txtPass.setText("1234");
        txtPass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPassActionPerformed(evt);
            }
        });
        add(txtPass);
        txtPass.setBounds(540, 560, 270, 30);

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/fondoLogin.png"))); // NOI18N
        add(jLabel5);
        jLabel5.setBounds(0, 0, 900, 700);
    }// </editor-fold>//GEN-END:initComponents

    
    // <editor-fold defaultstate="collapsed" desc="Eventos de UI">
    
    private void btnIngresarMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnIngresarMouseEntered
        this.lblMensaje.setVisible(true);
    }//GEN-LAST:event_btnIngresarMouseEntered

    private void btnIngresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIngresarActionPerformed
        login();
    }//GEN-LAST:event_btnIngresarActionPerformed

    private void btnIngresarMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnIngresarMouseExited
        this.lblMensaje.setVisible(false);
    }//GEN-LAST:event_btnIngresarMouseExited

    private void btnIngresarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnIngresarMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_btnIngresarMouseClicked

    private void txtPassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPassActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPassActionPerformed
    
    //</editor-fold>
    
    private void login(){
        String user = this.txtUser.getText();
        String pass = this.txtPass.getText();
        
        
        System.out.println(pass);
        _ControladorUsuarios = ControladorUsuarios.getInstancia();
        if(_ControladorUsuarios.validarUser(user, pass)){
            this.setVisible(false);
            _PanelPrincipal = PanelPrincipal.getInstancia();
            _PanelPrincipal.setVisible(true);
        }else
        {
            JOptionPane.showMessageDialog(null, MENSAJE_ERROR_LOGIN, TITULO_ERROR_LOGIN, JOptionPane.ERROR_MESSAGE);
        }
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    javax.swing.JButton btnIngresar;
    javax.swing.JLabel jLabel3;
    javax.swing.JLabel jLabel4;
    javax.swing.JLabel jLabel5;
    javax.swing.JLabel lblMensaje;
    javax.swing.JPasswordField txtPass;
    javax.swing.JTextField txtUser;
    // End of variables declaration//GEN-END:variables
}
